package com.hzcx.app.simplechat.ui.chat;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.chat.ChatUtil;
import com.hzcx.app.simplechat.chat.constant.ChatConstant;
import com.hzcx.app.simplechat.ui.chat.adapter.ChatSearchMsgAdapter;
import com.hzcx.app.simplechat.ui.chat.event.ChatSearchMsgEvent;
import com.hzcx.app.simplechat.ui.user.bean.UserInfoBean;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;
import com.hzcx.app.simplechat.util.log.LogUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 查找聊天记录
 */
public class ChatSearchMsgActivity extends BaseActivity {

    public static final String INTENT_USERINFO = "INTENT_USERINFO";

    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.iv_clear)
    ImageView ivClear;
    @BindView(R.id.tv_finish)
    TextView tvFinish;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.rv_search_msg)
    RecyclerView rvSearchMsg;

    private List<EMMessage> listData;
    private ChatSearchMsgAdapter msgAdapter;
    private EMConversation conversation;

    private List<EMMessage> searchList;

    private UserInfoBean userInfoBean;

    @Override
    public int getLayout() {
        return R.layout.activity_chat_search_msg;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {

        userInfoBean= (UserInfoBean) getIntent().getSerializableExtra(INTENT_USERINFO);

        listData = new ArrayList<>();
        msgAdapter = new ChatSearchMsgAdapter(R.layout.rv_item_chat_search_msg, listData, userInfoBean.getAvatar(),
                EmptyUtils.isEmpty(userInfoBean.getRemarks_name())?userInfoBean.getNickname():userInfoBean.getRemarks_name());
        rvSearchMsg.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        rvSearchMsg.setAdapter(msgAdapter);

        conversation = EMClient.getInstance().chatManager().getConversation(userInfoBean.getHxusername());

        searchList = new ArrayList<>();
        searchList.addAll(conversation.searchMsgFromDB(EMMessage.Type.TXT, System.currentTimeMillis(), conversation.getAllMsgCount(),
                null, EMConversation.EMSearchDirection.UP));

        msgAdapter.setOnItemClickListener((adapter, view, position) -> {
            EventBus.getDefault().post(new ChatSearchMsgEvent(listData.get(position)));
            finish();
        });
    }

    @Override
    public void initData() {
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (etSearch.getText().toString().length() > 0) {
                    ivClear.setVisibility(View.VISIBLE);
                    search();
                } else {
                    ivClear.setVisibility(View.GONE);
                    tvNum.setVisibility(View.GONE);
                    rvSearchMsg.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @OnClick({R.id.tv_finish, R.id.iv_clear})
    public void viewOnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_finish:
                finish();
                break;

            case R.id.iv_clear:
                etSearch.setText("");
                break;
        }
    }

    private void search() {
        List<EMMessage> list = new ArrayList<>();
        for (EMMessage message : searchList) {
            EMTextMessageBody txtBody = (EMTextMessageBody) message.getBody();
            if (txtBody.getMessage().contains(etSearch.getText().toString())) {
                list.add(message);
            }
        }
        listData.clear();
        listData.addAll(list);
        msgAdapter.setKey(etSearch.getText().toString());
        msgAdapter.notifyDataSetChanged();
        tvNum.setText("共有" + list.size() + "条相关的聊天记录");
        tvNum.setVisibility(View.VISIBLE);
        rvSearchMsg.setVisibility(View.VISIBLE);
    }

}
